<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="新产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name" :rules="[{ required: true, message: '请输入新产品名称!'}]">
              <a-input v-model="model.name" placeholder="请输入新产品名称" style="width: 100%;"></a-input>
            </a-form-model-item>
            <div class="has-error tip">
              <div class="ant-form-explain">注意：复制产品名称不可与已有产品名称重复</div>
            </div>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
  import { getAction } from '@/api/manage'
  export default {
    name: 'LogisticsCopyProductForm',
    mixins: [],
    components: {},
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 7 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {},
        url: {
          copyProduct: "/logistics/product/logisticsProduct/copyProduct",
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled;
      }
    },
    created () {
      //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        let currentName = this.model.name && this.model.name.slice(this.model.name.indexOf('-'));
        this.model.name = `【复制】${currentName}`;
      },
      handleOk_(){
        this.$refs.form.validate(valid => {
          if(valid){
            let params = {
              name: this.model.name
            }
            getAction(`${this.url.copyProduct}/${this.model.id}`, params).then((res) => {
              if(res.success){
                this.$message.success(res.message);
                this.$emit('ok');
              }else{
                this.$message.warning(res.message);
              }
            })
          }
        })
      }
    }
  }
</script>

<style scoped>
.tip{
  margin: -20px 0 0 20px;
}
</style>